//
//  ViewController.m
//  08-HomeWork
//
//  Created by dllo on 16/7/25.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "ViewController.h"
#import "informationViewController.h"
#import "addViewController.h"
#define kTableViewReuse @"reuseIdentifier"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate, addMessage>
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"DLS160710" ofType:@"plist"];
    self.array = [NSMutableArray arrayWithContentsOfFile:path];
    [self createTableView];
    [self createButton];
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 64)];
    view1.backgroundColor = [UIColor colorWithRed:0.961 green:0.966 blue:0.884 alpha:1.000];
    [self.view addSubview:view1];
    // 设置navigationbar的属性
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    self.navigationController.navigationBar.translucent = YES;
    // 添加编辑按钮
    self.navigationItem.leftBarButtonItem = self.editButtonItem;

}
#pragma mark - 创建tableView
- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.backgroundColor = [UIColor colorWithRed:0.961 green:0.966 blue:0.884 alpha:1.000];

    // 签订代理人
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTableViewReuse];
 }
#pragma mark - dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewReuse forIndexPath:indexPath];
    cell.textLabel.text = [_array[indexPath.row] objectForKey:@"name"];
    cell.backgroundColor = [UIColor colorWithRed:0.961 green:0.966 blue:0.884 alpha:1.000];
    return cell;
}
#pragma mark - delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    informationViewController *informationView = [[informationViewController alloc] init];
    informationView.string_number = [_array[indexPath.row] objectForKey:@"phoneNumber"];
    informationView.string_age = [[[NSNumberFormatter alloc]init]stringFromNumber:[_array[indexPath.row] objectForKey:@"age"]];
    informationView.string_gender = [_array[indexPath.row] objectForKey:@"gender"];
    informationView.string_name = [_array[indexPath.row] objectForKey:@"name"];
    [self.navigationController pushViewController:informationView animated:YES];
}
#pragma mark - CreateButton
- (void)createButton {
    self.navigationItem.title = @"通讯录";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addMessage:)];
}
#pragma mark - 实现bar上的按钮的点击事件
- (void)addMessage:(UIBarButtonItem *)barButton {
    addViewController *addView = [[addViewController alloc] init];
    addView.delegate = self;
    [self.navigationController pushViewController:addView animated:YES];
}
#pragma mark - 实现自定义协议方法
- (void)informationAddDictionary:(NSMutableDictionary *)dic {
    [_array addObject:dic];
    [_tableView reloadData];
}
#pragma mark - 添加滑动删除效果
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [_array removeObjectAtIndex:indexPath.row];
        [_tableView reloadData];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
